package com.ibest.accesssystem.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ibest.accesssystem.dto.input.PayChannelPermissionAssignmentInputDTO;
import com.ibest.accesssystem.entity.PayChannelPermissionAssignment;
import com.ibest.accesssystem.service.PayChannelPermissionAssignmentService;
import com.ibest.framework.common.enums.EnumsRtnMapResult;
import com.ibest.framework.common.persistence.BaseController;
import com.ibest.framework.common.utils.PageList;
import com.ibest.pay.entity.PayChannel;
import com.ibest.pay.entity.PayType;
import com.ibest.pay.service.PayChannelService;
import com.ibest.pay.service.PayTypeService;

@Controller
@RequestMapping(value="${adminPath}/accesssystem/payChannelPermissionAssignment")
public class PayChannelPermissionAssignmentController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(PayChannelPermissionAssignmentController.class);
	
	@Autowired
	private PayChannelPermissionAssignmentService payChannelPermissionAssignmentService;
	
	@Autowired
	private PayChannelService payChannelService;
	
	@Autowired
	private PayTypeService payTypeService;
	
	/**
	* 进入到列表页
	*/
	@RequestMapping(value="/")
	public String index(Model model){
		List<PayType> payTypeList = payTypeService.findByAll();
		model.addAttribute("payTypeList", payTypeList);
		return "module/accesssystem/payChannelPermissionAssignment/payChannelPermissionAssignmentList";
	}
	
	/**
	 * 进入到表单页-创建
	 */
	@RequestMapping(value="/add")
	public String add(){

		return "module/accesssystem/payChannelPermissionAssignment/payChannelPermissionAssignmentForm";
	}

	/**
	 * 进入到表单页-创建
	 */
	@RequestMapping(value="/permission")
	public String permission(@RequestParam String id, Model model) {
		model.addAttribute("systemSign", id);
		return "module/accesssystem/payChannelPermissionAssignment/payChannelPermissionAssignmentForm";
	}
	
	/**
	* 进入到表单页，编辑
	*/
	@RequestMapping(value="/edit")
	public String edit(@RequestParam String id, Model model){
		try {
			if(StringUtils.isNotEmpty(id)){
				PayChannelPermissionAssignment payChannelPermissionAssignment = payChannelPermissionAssignmentService.findById(id);
				if(payChannelPermissionAssignment != null){
					model.addAttribute("payChannelPermissionAssignment", payChannelPermissionAssignment);
				}else{
					setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "您查看的信息不存在！", model);
				}
			}else{
				setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "请选择需要编辑的信息！", model);
			}
		} catch (Exception e) {
			setRtnCodeAndMsg(EnumsRtnMapResult.EXCEPTION.getCode(), EnumsRtnMapResult.EXCEPTION.getMsg(), model);
		}
		return "module/accesssystem/payChannelPermissionAssignment/payChannelPermissionAssignmentForm";
	}
	
	/**
	* 进入到详情页
	*/
	@RequestMapping(value="/view")
	public String view(@RequestParam String id, Model model){
		try {
			if(StringUtils.isNotEmpty(id)){
				PayChannelPermissionAssignment payChannelPermissionAssignment = payChannelPermissionAssignmentService.findById(id);
				if(payChannelPermissionAssignment != null){
					model.addAttribute("payChannelPermissionAssignment", payChannelPermissionAssignment);
				}else{
					setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "您查看的信息不存在！", model);
				}
			}else{
				setRtnCodeAndMsg(EnumsRtnMapResult.FAILURE.getCode(), "请选择需要查看的信息！", model);
			}
		} catch (Exception e) {
			setRtnCodeAndMsg(EnumsRtnMapResult.EXCEPTION.getCode(), EnumsRtnMapResult.EXCEPTION.getMsg(), model);
		}
		return "module/accesssystem/payChannelPermissionAssignment/payChannelPermissionAssignmentDetail";
	}

	/**
	* 异步分页查询
	*/
	@ResponseBody
	@RequiresPermissions("payChannelPermissionAssignment:query")
	@RequestMapping(value="/list")
	public PageList<PayChannelPermissionAssignment> list(PayChannelPermissionAssignmentInputDTO payChannelPermissionAssignment, HttpServletRequest request){
		
		PageList<PayChannelPermissionAssignment> pageList = new PageList<PayChannelPermissionAssignment>();
		
		try {
			//设置分页参数
			super.setPage(request, pageList);
			pageList = payChannelPermissionAssignmentService.findByPage(pageList, payChannelPermissionAssignment);
			
			List<PayChannelPermissionAssignment> pcpaList = pageList.getRows();
			if(pcpaList!=null && pcpaList.size()>0) {
				for (PayChannelPermissionAssignment pp : pcpaList) {
					PayType pt = payTypeService.findById(pp.getPayTypeId());
					
					pp.setPayTypeName(pt.getPayName());
					pp.setPayChannelName(pt.getPayChannelId());
				}
			}
		} catch (Exception e) {
			logger.info("查询异常："+e.getMessage());
			e.printStackTrace();
		}
		return pageList;
	}

	/**
	* 异步表单提交
	*/
	@ResponseBody
	@RequiresPermissions("payChannelPermissionAssignment:create")
	@RequestMapping(value="create")
	public Map<String, Object> insert(PayChannelPermissionAssignment payChannelPermissionAssignment){

		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
		
		try {
			int result = payChannelPermissionAssignmentService.insert(payChannelPermissionAssignment);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("payChannelPermissionAssignment:update")
	@RequestMapping(value="update")
	public Map<String, Object> update(PayChannelPermissionAssignment payChannelPermissionAssignment){

		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
		
		try {
			int result = payChannelPermissionAssignmentService.update(payChannelPermissionAssignment);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("payChannelPermissionAssignment:updateIsOpen")
	@RequestMapping(value="/updateIsOpen")
	public Map<String, Object> updateIsOpen(PayChannelPermissionAssignment payChannelPermissionAssignment){
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "保存成功");
		
		try {
			int isOpen = payChannelPermissionAssignment.getIsOpen();
			payChannelPermissionAssignment = payChannelPermissionAssignmentService.findById(payChannelPermissionAssignment.getId());
			payChannelPermissionAssignment.setIsOpen(isOpen);
			int result = payChannelPermissionAssignmentService.update(payChannelPermissionAssignment);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByFailure(rtnMap, "保存失败");
		}
		return rtnMap;
	}
	
	@ResponseBody
	@RequiresPermissions("payChannelPermissionAssignment:delete")
	@RequestMapping(value="delete")
	public Map<String, Object> delete(@RequestParam(required=true) String ids){
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		setRtnCodeAndMsgBySuccess(rtnMap, "删除成功");
		
		try {
			int result = payChannelPermissionAssignmentService.deleteByIds(ids);
			if(result == 0){
				setRtnCodeAndMsgByFailure(rtnMap, "删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			setRtnCodeAndMsgByException(rtnMap, null);
		}
		return rtnMap;
	}
}
